<?php 

/**
 * Utility for paganation
 * @author Yiro
 *
 */
class PagerUtil
{
	// Current page
	var $CURRENT;

	// Maximum page
	var $MAX_PAGE;

	// Minimum page
	var $MIN_PAGE = 1;

	// Count of Page
	var $PAGE_COUNT;

	// Records per page
	var $PAGE_SIZE;
	
	//Count of Records
	var $RECORD_COUNT;

	// Offset of the first record of current page
	var $RECORD_OFFSET;

	// HTML Code to show the pagination
	var $html_str;
	
	// [1,2]
	var $MODE;

	function __construct( $current,$record_count,$page_size=PAGE_SIZE,$mode )
	{
		$this->html_str = '';
		$this->CURRENT = $current;
		$this->RECORD_COUNT = $record_count;
		$this->PAGE_SIZE = $page_size;
		$this->MODE = $mode;
		$this->MAX_PAGE = ceil($this->RECORD_COUNT/$this->PAGE_SIZE);
		$this->calc_reocord_offset();
	}

	function __destruct()
	{
		return true;
	}

	function calc_reocord_offset()
	{
		if($this->CURRENT<=1)
			$this->RECORD_OFFSET = 0;
		else
			$this->RECORD_OFFSET = ($this->CURRENT - 1) * $this->PAGE_SIZE;
	}

	function display($parameters='')
	{
		if ($this->MODE == 1)
		{
			$this->html_str = $this->show_mode_1($parameters);
		}
		else if($this->MODE == 2)
		{
			$this->html_str = $this->show_mode_2($parameters);
		}
		echo $this->html_str;
	}

	function show_mode_1($parameters)
	{
		$pre_page = ($this->CURRENT <= 1)? $this->MIN_PAGE : $this->CURRENT - 1;
		$next_page= ($this->CURRENT >= $this->PAGE_COUNT)? $this->PAGE_COUNT : $this->CURRENT + 1 ;

		$output_str = "";
		$output_str .= "第 $this->CURRENT/$this->PAGE_COUNT 页 共 $this->RECORD_COUNT 条记录 ";
		$output_str .= "<a href='?page=1".$parameters."'>首页</a> ";
		$output_str .= "<a href='?page=$pre_page".$parameters."'>前一页</a> ";
		$output_str .= "<a href='?page=$next_page".$parameters."'>后一页</a> ";
		$output_str .= "<a href='?page=$this->MAX_PAGE".$parameters."'>末页</a> ";

		return $output_str;
	}

	function show_mode_2($parameters)
	{
		$page_num_to_show = 3;
		$offset = ceil(($page_num_to_show-1)/2);

		$smallest;
		$largest;

		// 超过上限和下限
		if(($this->CURRENT-$offset<1) && ($this->CURRENT+$offset>$this->MAX_PAGE))
		{
			$smallest = $this->MIN_PAGE;
			$largest = $this->MAX_PAGE;
		}
		// 超过下限，但没有超过上限
		else if(($this->CURRENT-$offset<1) && ($this->CURRENT+$offset<=$this->MAX_PAGE))
		{
			$remaining = $offset - ($this->CURRENT - $this->MIN_PAGE);
			$smallest = 1;
			$largest = (($this->CURRENT + $offset + $remaining > $this->MAX_PAGE)? $this->MAX_PAGE:($this->CURRENT + $offset + $remaining));
		}
		// 超过上限，但没有超过下限
		else if(($this->CURRENT-$offset>=1) && ($this->CURRENT+$offset>$this->MAX_PAGE))
		{
			$remaining = $offset - ($this->MAX_PAGE - $this->CURRENT);
			$largest = $this->MAX_PAGE;
			$smallest = (($this->CURRENT - $offset - $remaining) < $this->MIN_PAGE)? $this->MIN_PAGE:($this->CURRENT - $offset - $remaining);
		}
		// 上下限都没有超过
		else
		{
			$smallest = $this->CURRENT - $offset;
			$largest = $this->CURRENT + $offset;
		}

		$output_str = "";
		$pre_page = ($this->CURRENT <= 1)? $this->MIN_PAGE : $this->CURRENT - 1;
		$next_page= ($this->CURRENT >= $this->PAGE_COUNT)? $this->PAGE_COUNT : $this->CURRENT + 1 ;
		
		if($this->CURRENT>$this->MIN_PAGE)
		{
			$output_str = $output_str."<a href=\"?page=$pre_page".$parameters."\" class=\"style-1-a\">&laquo;</a>";
		}
		
		while($smallest<=$largest)
		{
			if($smallest==$this->CURRENT)
			{
				$output_str = $output_str."<span class='style-1-current'>$this->CURRENT</span>";
			}
			else
			{
				$output_str = $output_str."<a href=\"?page=$smallest".$parameters."\" class=\"style-1-a\">$smallest</a>";
			}
			
			$smallest++;
		}
		
		if($this->CURRENT<$this->MAX_PAGE)
		{
			$output_str = $output_str."<a href=\"?page=$next_page".$parameters."\" class=\"style-1-a\">&raquo;</a>";
		}
		
		return $output_str;
	}
}
?>